/*
 * IkaCore.cpp
 */

#include "IkaCore.h"
#include"define.h"

using namespace atomic;

namespace atomic {

//*********************************************************************
IkaCore::IkaCore() {
	Init();
}

//*********************************************************************
IkaCore::IkaCore(const IkaCore & copy) {
	Init();
	this->myHeight = copy.myHeight;
	this->myWidth = copy.myWidth;
	this->myScrollFactor = copy.myScrollFactor;
	this->myX = copy.myX;
	this->myY = copy.myY;
	this->isActive = copy.isActive;
	this->isDead = copy.isDead;
	this->isExistent = copy.isExistent;
	this->isVisible = copy.isVisible;
	this->myMaxAnims = copy.myMaxAnims;
	if (myMaxAnims > MAX_ANIMS) {
		myMaxAnims = MAX_ANIMS;
	}
	this->myAnimsCount = copy.myAnimsCount;

	myAnims = new IkaAnim*[MAX_ANIMS];
	for (unsigned int i = 0; i < myMaxAnims; i++) {
		myAnims[i] = new IkaAnim(*(copy.myAnims[i]));
	}
}

//*********************************************************************
IkaCore::IkaCore(float x, float y, IkaAnim* anim, unsigned int width,
		unsigned int height, float scrollFactor, bool exists, bool active,
		bool dead, bool visible, unsigned int maxAnims, unsigned int animsCount) {
	Init();
	this->myHeight = height;
	this->myWidth = width;
	this->myScrollFactor = scrollFactor;
	this->myX = x;
	this->myY = y;
	this->isActive = active;
	this->isDead = dead;
	this->isExistent = exists;
	this->isVisible = visible;
	this->myMaxAnims = maxAnims;
	if (myMaxAnims > MAX_ANIMS) {
		myMaxAnims = MAX_ANIMS;
	}
	this->myAnimsCount = animsCount;

	myAnims = new IkaAnim*[MAX_ANIMS];
	for (unsigned int i = 0; i < myMaxAnims; i++) {
		myAnims[i] = new IkaAnim(anim[i]);
	}
}

//*********************************************************************
void IkaCore::Init() {
	this->myHeight = 16;
	this->myWidth = 16;
	this->myScrollFactor = 1.0;
	this->myX = 10;
	this->myY = 10;
	this->isActive = true;
	this->isDead = false;
	this->isExistent = true;
	this->isVisible = true;

	myMaxAnims = MAX_ANIMS;
	myAnimsCount = 0;
	myAnims = new IkaAnim*[MAX_ANIMS];
	for (unsigned int i = 0; i < myMaxAnims; i++) {
		myAnims[i] = new IkaAnim();
	}
}

//*********************************************************************
IkaCore::~IkaCore() {
	delete[] myAnims;
}

//*********************************************************************
void IkaCore::Update() {
}

//*********************************************************************
void IkaCore::Render() {
}

//*********************************************************************
/*
 bool IkaCore::HitWall() {
 return true;
 }
 */

//*********************************************************************
bool IkaCore::Overlaps(const atomic::IkaCore & core) {
	return true;
}

//*********************************************************************
/*
 bool IkaCore::HitFloor() {
 return true;
 }
 */

//*********************************************************************
bool IkaCore::OverlapsPoint(float x, float y) {
	return true;
}

//*********************************************************************
/*
 bool IkaCore::Flickering() {
 return true;
 }
 */

//*********************************************************************
/*
 bool IkaCore::OnScreen() {
 return true;
 }
 */

//*********************************************************************
void IkaCore::Kill() {
}

//*********************************************************************
/*
 void IkaCore::Flicker(float duration) {
 }
 */

//*********************************************************************
/*
 bool IkaCore::HitCeiling() {
 return true;
 }
 */

//*********************************************************************
bool IkaCore::Active() {
	return isActive;
}

//*********************************************************************
bool IkaCore::Dead() {
	return isDead;
}

//*********************************************************************
bool IkaCore::Existent() {
	return isExistent;
}

//*********************************************************************
bool IkaCore::Visible() {
	return isVisible;
}

//*********************************************************************
int IkaCore::Height() {
	return myHeight;
}

//*********************************************************************
float IkaCore::ScrollFactor() {
	return myScrollFactor;
}

//*********************************************************************
int IkaCore::Width() {
	return myWidth;
}

//*********************************************************************
float IkaCore::X() {
	return myX;
}

//*********************************************************************
float IkaCore::Y() {
	return myY;
}

//*********************************************************************
IkaAnim** IkaCore::Anims() {
	return myAnims;
}
//*********************************************************************
void IkaCore::SetActive(bool active) {
	this->isActive = active;
}

//*********************************************************************
void IkaCore::SetDead(bool dead) {
	this->isDead = dead;
}

//*********************************************************************
void IkaCore::SetExistent(bool exists) {
	this->isExistent = exists;
}

//*********************************************************************
void IkaCore::SetVisible(bool visible) {
	this->isVisible = visible;
}

//*********************************************************************
void IkaCore::SetHeight(unsigned int height) {
	this->myHeight = height;
}

//*********************************************************************
void IkaCore::SetScrollFactor(float scrollFactor) {
	this->myScrollFactor = scrollFactor;
}

//*********************************************************************
void IkaCore::SetWidth(unsigned int width) {
	this->myWidth = width;
}

//*********************************************************************
void IkaCore::SetX(float x) {
	this->myX = x;
}

//*********************************************************************
void IkaCore::SetY(float y) {
	this->myY = y;
}

//*********************************************************************
bool IkaCore::SetAnim(IkaAnim* anim, unsigned int index) {
	if (index < myMaxAnims) {
		try {
			// append it to the array
			myAnims[index] = new IkaAnim();
			myAnims[index] = anim;
			return true;
		} catch (int e) {
			printf("SetAnim failed \n");
			return false;
		}
	} else {
		printf("SetAnim failed: maxAnim \n");
		return false;
	}
}

//*********************************************************************
void IkaCore::AddAnim(IkaString name) {

	// 01 - creation of a new anim
	if (myAnimsCount < myMaxAnims) {
		// creation of an anim
		IkaAnim *anim = NULL;
		anim = new IkaAnim();

		// 02 - files existence test
		char chSrc[50];
		int ref = 0;
		sprintf(chSrc, "%s%s%d.png", SRC_IMG ,name.Text(), ref);
		IkaString isSrc(chSrc);
		printf("[%s] \n", chSrc);

		FILE* file = NULL;	//TODO faire une fonction de test
		file = fopen(chSrc, "r");

		while (file != NULL) {

			fclose(file);
			ref++;

			// AddFrame to current anim
			anim->AddFrame(isSrc);
			printf("Hello File");
		}
		fclose(file);
	}
}

//*********************************************************************
atomic::IkaCore& IkaCore::operator=(const atomic::IkaCore & core) {
	this->myHeight = core.myHeight;
	this->myWidth = core.myWidth;
	this->myScrollFactor = core.myScrollFactor;
	this->myX = core.myX;
	this->myY = core.myY;
	this->isActive = core.isActive;
	this->isDead = core.isDead;
	this->isExistent = core.isExistent;
	this->isVisible = core.isVisible;
	this->myMaxAnims = core.myMaxAnims;
	if (myMaxAnims > MAX_ANIMS) {
		myMaxAnims = MAX_ANIMS;
	}
	this->myAnimsCount = core.myAnimsCount;

	myAnims = new IkaAnim*[MAX_ANIMS];
	for (unsigned int i = 0; i < myMaxAnims; i++) {
		myAnims[i] = new IkaAnim(*(core.myAnims[i]));
	}

	return *this;
}
//*********************************************************************



//*********************************************************************

}
